﻿using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using DE.Sdc.SharePoint.Common.Deployment.FileExtensionActions;

namespace DE.Sdc.SharePoint.Common.Deployment
{
    public class ContentStructureImporter
    {
        protected internal static SPFeatureReceiverProperties ReceiverProperties { get; private set; }
        private readonly string _contentStructureDescriptionXmlPath;
        protected static internal SPWeb FeatureWeb { get; private set; }
        private ContentStructure _contentStructure;
        protected static internal FileExtensionActionDictionary _fileExtensionActions = new FileExtensionActionDictionary();
        private PropertyInfo _currentContextProperty;
        protected static internal bool CurrentContextInitiallySet { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="contentStructureDescriptionXmlPath">Absolute path to the content structure xml file</param>
        /// <param name="receiverProperties">Feature receiver properties</param>
        private ContentStructureImporter(string contentStructureDescriptionXmlPath, SPFeatureReceiverProperties receiverProperties)
        {
            _contentStructureDescriptionXmlPath = contentStructureDescriptionXmlPath;
            ReceiverProperties = receiverProperties;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="contentStructureDescriptionXmlPath">Absolute path to the content structure xml file</param>
        /// <param name="webUrl">Absolute Url to the Web</param>
        public ContentStructureImporter(string contentStructureDescriptionXmlPath, string webUrl)
        {
            _contentStructureDescriptionXmlPath = contentStructureDescriptionXmlPath;
            using (SPSite site = new SPSite(webUrl))
            {
                FeatureWeb = site.OpenWeb();
            }
        }

        /// <summary>
        /// Starts the content structure import
        /// </summary>
        public void Execute()
        {
            ContentStructure structure;
            var serializer = new XmlSerializer(typeof(ContentStructure));
            FileStream importXml = System.IO.File.OpenRead(_contentStructureDescriptionXmlPath);

            try
            {
                _contentStructure = (ContentStructure)serializer.Deserialize(importXml);
            }
            catch(Exception ex)
            {
                Trace.WriteLine(ex.Message, "ContentStructureImporter::Execute:DeserializeXml");
                throw;
            }
            finally
            {
                importXml.Close();
            }

            try
            {
                if (FeatureWeb == null)
                    FeatureWeb = DeploymentHelper.GetWeb(ReceiverProperties.Feature.Parent);
                CurrentContextInitiallySet = SPContext.Current != null;
                LoadFileExtensionActions();
                Web.ImportWeb(_contentStructure, FeatureWeb, FeatureWeb.IsRootWeb ? null : FeatureWeb.ParentWeb);
                _fileExtensionActions.Execute();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message, "ContentStructureImporter::Execute:ImportWeb");
                throw;
            }
            
        }

        /// <summary>
        /// Starts the content structure import
        /// </summary>
        /// <param name="contentStructureDescriptionXmlPath">Absolute path to the content structure xml file</param>
        /// <param name="receiverProperties">Feature receiver properties</param>
        public static void Execute(string contentStructureDescriptionXmlPath, SPFeatureReceiverProperties receiverProperties)
        {
            var importer = new ContentStructureImporter(contentStructureDescriptionXmlPath, receiverProperties);
            importer.Execute();
        }
        
        /// <summary>
        /// Starts the content structure import
        /// </summary>
        /// <param name="contentStructureDescriptionXmlPath">Absolute path to the content structure xml file</param>
        /// <param name="webUrl">Absolute Url to the Web</param>
        public static void Execute(string contentStructureDescriptionXmlPath, string webUrl)
        {
            var importer = new ContentStructureImporter(contentStructureDescriptionXmlPath, webUrl);
            importer.Execute();
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadFileExtensionActions()
        {
            if (_contentStructure.FileExtensionActions != null)
            {
                for (int index = 0; index < _contentStructure.FileExtensionActions.Length; index++)
                {
                    Type fileExtensionActionType = Type.GetType(_contentStructure.FileExtensionActions[index]);
                    _fileExtensionActions.Add(fileExtensionActionType, ReceiverProperties);
                }
            }
        }

        

        

        

        
    }
}
